package com.study.cloudAlibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.study.cloudAlibaba.controller.handler.CustomerBlockHandle;
import com.study.springcloud.entities.CommonResult;
import com.study.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lzc
 * @date 2022/7/28 11:43
 */
@RestController
public class RateLimitController {

    @GetMapping("byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommonResult byResource(){
        CommonResult commonResult = new CommonResult(
                200,
                "按资源名称测试限流 OK",
                new Payment(2022L, "testtest")
        );
        return commonResult;
    }
    public CommonResult handleException(BlockException e){
        return new CommonResult(
                444,
                e.getClass().getCanonicalName()+"\t 服务不可用"
        );
    }
    @GetMapping("/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult byUrl()
    {
        return new CommonResult(200,"按url限流测试OK",new Payment(2022L,"test02"));
    }

    @GetMapping("customerHandler")
    @SentinelResource(
            value = "customerHandler",
            blockHandlerClass = CustomerBlockHandle.class,
            blockHandler = "handleException2"
    )
    public CommonResult testCustomerBlockHandler(){
        return new CommonResult(200,"testCustomerBlockHandler OK!");
    }

}
